package shop.maytinh.view;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import shop.maytinh.model.vo.*;
import shop.maytinh.model.*;
import shop.maytinh.util.SendMail;
import shop.maytinh.controller.*;



public class BuyNow extends AbstractClass {
	private Integer idbuyProduct;
	private Integer warranty;
	private String warranty1;
	private Card card;
	Product product;
	ProductManager pmn = new ProductManager();
	CardManager cmn = new CardManager();
	ArrayList<Card> lstarraycard = new ArrayList<Card>();
	List<Card> listcard ;
	private int tongtien;
	private String thongbao;
	private Oder oder;
	private Detail_oder detaioder;
	private Member member1;
	private String messenger;
	
	private List<Card> lstcard;
	
	public int kimtratrung(int id,ArrayList<Card> lst)
	{
		for(int i=1; i<=lst.size();i++)
		{
			int a = lst.get(i-1).getProduct().getId();
			if(a == id)
			{
				return i;
			}			
		}
		
		return 0;
	}
	static Boolean isIntegerParseInt(String str) {
	    try {
	    	Integer.parseInt(str);
	        return true;
	    } catch (Exception e){}
	    return false;
	}

	public String execute() {
		try{
			if(!isIntegerParseInt(warranty1))
			{
				messenger = "Please enter the number";
				return "success1";
			}
			warranty =	Integer.parseInt(warranty1);
		product = pmn.getProductById(idbuyProduct);
		Card card = new Card();
		
		card.setProduct(product);			
		card.setWarranty(warranty);
		
		//lstarraycard.add(card);
		
				
		if ( (ArrayList<Card>) request.getSession().getAttribute("sslistcard") != null)
		{
			lstarraycard = (ArrayList<Card>) request.getSession().getAttribute("sslistcard");
			
			if(kimtratrung(product.getId(),lstarraycard)!=0)
			{
				int temp = kimtratrung(product.getId(),lstarraycard);
				int w  = lstarraycard.get(temp-1).getWarranty() +warranty;
				lstarraycard.get(temp-1).setWarranty(w);
			}
			else {
				lstarraycard.add(card);
			}
		request.getSession().setAttribute("sslistcard", lstarraycard);	
			
		}
		else {
			lstarraycard.add(card);
			request.getSession().setAttribute("sslistcard", lstarraycard);		
		}

		listcard = lstarraycard;
		
		for(int j=0;j<lstarraycard.size();j++)
		{
			tongtien = tongtien + ((lstarraycard.get(j).getProduct().getPice()) * (lstarraycard.get(j).getWarranty()));
		}
		
		return "success";
		
		}
		catch(Exception e){
			e.printStackTrace();		
			return "error";
		}						
	}
	public String go1(){
		lstarraycard = (ArrayList<Card>) request.getSession().getAttribute("sslistcard");
		
		if(lstarraycard != null)
		{
			listcard = lstarraycard;
			for(int j=0;j<lstarraycard.size();j++)
			{
				tongtien = tongtien + ((lstarraycard.get(j).getProduct().getPice()) * (lstarraycard.get(j).getWarranty())) +3;
			}
			return "success";
		}
			
		else 
			return "null";
	}
	public String go()
	{
		
		try{
		if ( member!= null)
		{
			lstarraycard = (ArrayList<Card>) request.getSession().getAttribute("sslistcard");
			listcard = lstarraycard;
			for(int j=0;j<lstarraycard.size();j++)
			{
				tongtien = tongtien + ((lstarraycard.get(j).getProduct().getPice()) * (lstarraycard.get(j).getWarranty())) +3;
			}
		return "success";
		}
		else{
			return "null";
		}
		}
		catch(Exception e)
		{
		return "error";
		}
			
	}
	public static String convertDateToString(Date date, String format) {
	    SimpleDateFormat mySimpleDateFormat = new SimpleDateFormat(format);
	    return mySimpleDateFormat.format(date);
	}
	public String comfim()
	{
		try{
			String a = "WELLCOME ";
			member1= new Member();
			member1 =  (Member) request.getSession().getAttribute("Member");
			int c = member1.getId();
			a = a + member1.getFullname() + " GO ON SHOP COMPUTER...!"+"\n";
			if ( (Member) request.getSession().getAttribute("Member") != null)
			{
				lstarraycard = (ArrayList<Card>) request.getSession().getAttribute("sslistcard");
				listcard = lstarraycard;
				for(int j=0;j<lstarraycard.size();j++)
				{
					tongtien = tongtien + ((lstarraycard.get(j).getProduct().getPice()) * (lstarraycard.get(j).getWarranty())) +3;
				}
				thongbao = "Congratulations Order Success!";
				//xu ly oder and detaioder
				oder = new Oder();
				oder.setMember(member1);
				
				String format1 = "dd/MM/yyyy";
				Date d = new Date();
				String date1 = convertDateToString(d,format1);
				
				DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
				Date myDate = formatter.parse(date1);
				java.sql.Date sqlDate = new java.sql.Date(myDate.getTime());
				
				oder.setCreated_at(sqlDate);
				oder.setDate_delivery(sqlDate);
				oder.setDate_oder(sqlDate);
				oder.setUpdated_at(sqlDate);
				
				oder.setStatus(true);
				
				OderManager omn = new OderManager();
				omn.addOder(oder);				
				 a = a +"YOUR ODER\n";
				for(int k=0;k<lstarraycard.size();k++)
					{
					product = new Product();
					int e = lstarraycard.get(k).getProduct().getId();
					product = pmn.getProductById(e);
					
					int sl = lstarraycard.get(k).getWarranty();
					
					int ttien = (lstarraycard.get(k).getProduct().getPice()) * sl;
						//add detailOder
						detaioder = new Detail_oder();
						
						detaioder.setOder(oder);
						detaioder.setProduct(product);
						detaioder.setQuantum(sl);
						detaioder.setTotal_money(ttien);
						a = a+(k+1)+"\t"+ product.getName() +"\t\t"+product.getPice()+"\n";
						
						detaioder.setCreated_at(sqlDate);
						detaioder.setUpdated_at(sqlDate);
						
						
						omn.addDetailOder(detaioder);					
						//tongtien = tongtien + ((lstarraycard.get(j).getProduct().getPice()) * (lstarraycard.get(j).getWarranty())) +3;
					}		
				a = a +"Total : \t"+tongtien+"\n"+"THANK YOU! WE WILL CONTACT AGAIN SOON AS SOON THE COMFIM YOUR ODER...!";
				//ket thuc xu ly oder
				String subject="Administrator Shop Online Computer ";
				String content=a;
				try{
				SendMail.sendmail("nguyendiemit92@gmail.com", "nguyendiemit", member.getMail(), subject, content);
				}
				catch(Exception e)
				{
					thongbao = "";
					messenger ="Failure to send mail, please check internet connection...!";
					return "errorInternet";
				}
				request.getSession().removeAttribute("sslistcard");
			return "success";
			}
			else{
				thongbao = " Order Fail!";
				return "null";
			}
			}
			catch(Exception e)
			{
				thongbao = " Order Fail!";
				return "error";			
			}				
	}
	public Integer getIdbuyProduct() {
		return idbuyProduct;
	}
	public void setIdbuyProduct(Integer idbuyProduct) {
		this.idbuyProduct = idbuyProduct;
	}
	public Card getCard() {
		return card;
	}
	public void setCard(Card card) {
		this.card = card;
	}
	public ProductManager getPmn() {
		return pmn;
	}
	public void setPmn(ProductManager pmn) {
		this.pmn = pmn;
	}
	public CardManager getCmn() {
		return cmn;
	}
	public void setCmn(CardManager cmn) {
		this.cmn = cmn;
	}
	public List<Card> getLstcard() {
		return lstcard;
	}
	public void setLstcard(List<Card> lstcard) {
		this.lstcard = lstcard;
	}
	public Product getProduct() {
		return product;
	}
	public void setProduct(Product product) {
		this.product = product;
	}
	public ArrayList<Card> getLstarraycard() {
		return lstarraycard;
	}
	public void setLstarraycard(ArrayList<Card> lstarraycard) {
		this.lstarraycard = lstarraycard;
	}
	public List<Card> getListcard() {
		return listcard;
	}
	public void setListcard(List<Card> listcard) {
		this.listcard = listcard;
	}

	public Integer getWarranty() {
		return warranty;
	}

	public void setWarranty(Integer warranty) {
		this.warranty = warranty;
	}

	public int getTongtien() {
		return tongtien;
	}

	public void setTongtien(int tongtien) {
		this.tongtien = tongtien;
	}

	public String getThongbao() {
		return thongbao;
	}

	public void setThongbao(String thongbao) {
		this.thongbao = thongbao;
	}

	public Oder getOder() {
		return oder;
	}

	public void setOder(Oder oder) {
		this.oder = oder;
	}

	public Detail_oder getDetaioder() {
		return detaioder;
	}

	public void setDetaioder(Detail_oder detaioder) {
		this.detaioder = detaioder;
	}

	public Member getMember1() {
		return member1;
	}

	public void setMember1(Member member1) {
		this.member1 = member1;
	}

	public String getWarranty1() {
		return warranty1;
	}

	public void setWarranty1(String warranty1) {
		this.warranty1 = warranty1;
	}
	public String getMessenger() {
		return messenger;
	}
	public void setMessenger(String messenger) {
		this.messenger = messenger;
	}
	
	
}
